package com.tz.farmer.config;

import com.alibaba.xxpt.gateway.shared.client.http.ExecutableClient;
import com.alibaba.xxpt.gateway.shared.client.http.GetClient;
import com.alibaba.xxpt.gateway.shared.client.http.PostClient;

import java.util.Map;


public class ExecutableClientConfig {
    private static ExecutableClient executableClient = null;

    static {
        try {
            executableClient = ExecutableClient.getInstance();
//            executableClient.setAccessKey("tyl_login_dingoa-8Vs5q2kvayqqy");
//            executableClient.setSecretKey("113vj5G6El4W7917g2f659MzFSog9XyU8ZCpJcw4");
//            executableClient.setDomainName("openplatform.dg-work.cn");

            executableClient.setAccessKey("tzscjg_tyl_dingoa-7GGwwja2YvMv");
            executableClient.setSecretKey("F39FJwe5k77T7253O8w2u696xleRS1s3zPOdvW9M");
            executableClient.setDomainName("openplatform-pro.ding.zj.gov.cn");
            executableClient.setProtocal("https");
            executableClient.init();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    public static String doGet(String api,Map<String,String> map) {
        try {
            GetClient getClient = executableClient.newGetClient(api);
            //设置参数
            for (Map.Entry<String, String> entry : map.entrySet()) {
                getClient.addParameter(entry.getKey(),entry.getValue());
            }
            //调用API
            String apiResult = getClient.get();
            return  apiResult;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    public static String doPost(String api,Map<String,String> map) {
        try {
            PostClient postClient = executableClient.newPostClient(api);
            //设置参数
            for (Map.Entry<String, String> entry : map.entrySet()) {
                postClient.addParameter(entry.getKey(),entry.getValue());
            }
            //调用API
            String apiResult = postClient.post();
            return  apiResult;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }
}
